package com.et.machine

import com.et.drive.info.ChannelInfo
import com.et.machine.store.ChannelEntity
import com.et.machine.store.ENABLED
import com.et.util.endInsert2String
import com.et.util.headInsert2String
import org.junit.Assert.assertEquals
import org.junit.Test
import java.math.BigDecimal
import java.text.SimpleDateFormat
import java.util.*
import kotlin.experimental.and
import kotlin.experimental.inv
import kotlin.math.ceil

/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
class ExampleUnitTest {
    @Test
    fun addition_isCorrect() {
        assertEquals(4, 2 + 2)
    }

    @Test
    fun format(){
        var text = "2.3"
        val toInt = text.toBigDecimal().multiply(BigDecimal(100)).toInt()
        println(toInt)
        println(String.format("%.2f", (toInt / 100f)))
        println(SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault()).format(System.currentTimeMillis()))
    }

    @Test
    fun test(){
        val it:ByteArray = byteArrayOf(1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
        val stringBuilder = StringBuilder(512)
        it.copyOfRange(1, 7).forEach {
                val binary = Integer.toBinaryString(((it and 0xFF.toByte()).inv() + 0x100))
                stringBuilder.append(binary.substring(binary.length - 8))
            println(it)
            }
            println(stringBuilder)
            stringBuilder.clear()
        var i = 0
            it.copyOfRange(7, it.size).forEach {
                stringBuilder.append((it.toInt() and 0xFF).headInsert2String('0', 2))
                i++
            }
            println(stringBuilder)
            println(i)
        println(it.size)

    }

    @Test
    fun binary(){
        fun uploadGoodsInfo(vararg channelEntity: ChannelEntity){
            var size = channelEntity.size
            for (sendCount in 0 until ceil(size / 28f).toInt()) {
                StringBuilder(1024).apply {
                    val singleSize = if (size >= 28) 28 else size
                    append(singleSize.headInsert2String('0', 3))
                    for (index in sendCount * 28 until sendCount * 28 + singleSize) {
                        append(channelEntity[index].box.headInsert2String('0', 2))
                        append(channelEntity[index].channel.headInsert2String('0', 2))
                        if (channelEntity[index].status == ENABLED) {
                            append(channelEntity[index].barcode.endInsert2String(' ', 16))
                        } else {
                            append("0238".endInsert2String(' ', 16))
                        }
                        append(channelEntity[index].priceCent.headInsert2String('0', 6))
                        append(channelEntity[index].discount.headInsert2String('0', 3))
                        append("00000")
                    }
                    println(this)
                    println("=========================================================")
                }
                size -= 28
            }
        }

        uploadGoodsInfo(*Array(48) { index ->
            ChannelEntity.create(11, index + 1)
        })
    }


    @Test
    fun channelNormal(){
        val data = "000011111110000000011111000000000000000000000000"
        val byteArray = byteArrayOf(11, -16, 31, -32, -1, -1, -1, -18, -18, -18, -18, -18, -18, 51, 40, 20, 17, 2, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
        val channelInfo = ChannelInfo(byteArray)
        for (channel in 1..48) {
            val byte = channelInfo.data[ceil(channel / 8f).toInt()]
            val c = Integer.toBinaryString((byte and 0xFF.toByte()) + 0x100)
            val c1 = c.substring(c.length - 8).toCharArray()[(channel - 1) % 8]
            print(c1)
        }
    }
}